package com.mmo.cq.game.script.proto.gm.controller;

import com.alibaba.fastjson.JSONObject;
import com.mmo.cq.game.script.proto.gm.GmMappingInfo;
import com.mmo.cq.game.script.proto.gm.GmModule;
import com.mmo.cq.game.script.proto.gm.message.ReqGm;
import com.mmo.cq.game.server.bean.user.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.wxd.net.SocketSession;
import org.wxd.net.controller.IController;
import org.wxd.net.controller.MessageController;
import org.wxd.net.controller.ann.ProtoController;
import org.wxd.net.controller.ann.ProtoMapping;

import javax.annotation.Resource;

/**
 * 请求执行gm命令--没有回复 file=Gm.proto
 *
 * @author: Troy.Chen(無心道, 15388152619)
 * @version: 2023/02/10 15:03:58
 */
@Slf4j
@ProtoController(service = "org.wxd.net.web.ws.WebSocketServer")
public final class ReqGmController implements IController {

    @Resource GmModule gmModule;

    /** 请求执行gm命令--没有回复 ReqGm */
    @ProtoMapping(remarks = "请求执行gm命令--没有回复")
    public void exec(ReqGm reqMessage, JSONObject param, SocketSession session) throws Exception {
        UserInfo userInfo = (UserInfo) param.get(MessageController.OBJ_Player);

        GmMappingInfo gmMappingInfo = gmModule.getGmMap().get(reqMessage.getName());
        if (gmMappingInfo == null) {
            return;
        }
        try {
            gmMappingInfo.getMethod().invoke(gmMappingInfo.getInstance(), userInfo, reqMessage.getParams().split(",|，"));
        } catch (Throwable throwable) {
            log.error("执行gm命令：" + userInfo.logName() + " - " + reqMessage.getName() + " - " + reqMessage.getParams(), throwable);
        }

    }

}
